home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 58678 / 58678.xpi / chrome / clipple.jar / content / statusbar.js < prev   
Text File  |  2010-01-13  |  6KB  |  177 lines

  1. /**
  2.  * @fileOverview StatusBar
  3.  * @name statusbar.js
  4.  * @author mooz <stillpedant@gmail.com>
  5.  * @license The MIT License
  6.  */
  7.  
  8. let clippleStatusbar =
  9.     (function () {
  10.          // Private {{ =============================================================== //
  11.  
  12.          let clip, util;
  13.  
  14.          function init() {
  15.              try
  16.              {
  17.                  Components.utils.import("resource://clipple-share/share.js", self.modules);
  18.  
  19.                  clip = self.modules.clip;
  20.                  util = self.modules.util;
  21.              }
  22.              catch (x) {}
  23.          }
  24.  
  25.          function $(aId) {
  26.              return document.getElementById(aId);
  27.          }
  28.  
  29.          function removeAllChilds(aElement) {
  30.              while (aElement.hasChildNodes())
  31.                  aElement.removeChild(aElement.firstChild);
  32.          }
  33.  
  34.          function updateActionMenuPopup(aPopup, aCommandString) {
  35.              for (let [i, text] in Iterator(clip.ring))
  36.              {
  37.                  let menuItem;
  38.                  menuItem = document.createElement("menuitem");
  39.  
  40.                  menuItem.setAttribute("label", (i + 1) + ". " + text);
  41.                  menuItem.setAttribute("oncommand", util.format(aCommandString, i));
  42.                  menuItem.setAttribute("tooltiptext", text);
  43.  
  44.                  aPopup.appendChild(menuItem);
  45.              }
  46.          }
  47.  
  48.          function updateDeleteMenuPopup(aPopup) {
  49.              let deleteAll = $("clipple-statusbar-menu-delete-all");
  50.  
  51.              Array.slice(aPopup.childNodes, 0).forEach(
  52.                  function (elem) {
  53.                      if (elem.localName !== "menuseparator" && elem !== deleteAll)
  54.                          aPopup.removeChild(elem);
  55.                  });
  56.  
  57.              updateActionMenuPopup(aPopup, "clippleStatusbar.removeNthItem(%s);");
  58.          }
  59.  
  60.          function truncate(aString, aLen) {
  61.              const len = aLen || 100;
  62.              let truncated = aString.substring(0, len);
  63.              return truncated + (aString.length !== truncated.length ? "..." : "");
  64.          }
  65.  
  66.          // Display {{ =============================================================== //
  67.  
  68.          const TIMEOUT_NORMAL = 2000;
  69.  
  70.          let msgTimeOut;
  71.  
  72.          function echo(aMsg, aTime) {
  73.              let statusBar = $('statusbar-display');
  74.  
  75.              if (!statusBar) return;
  76.  
  77.              if (msgTimeOut)
  78.              {
  79.                  clearTimeout(msgTimeOut);
  80.                  msgTimeOut = null;
  81.              }
  82.  
  83.              statusBar.label = aMsg;
  84.  
  85.              if (aTime)
  86.                  msgTimeOut = setTimeout(function () { echo('', 0); }, aTime);
  87.          }
  88.  
  89.          // }} ======================================================================= //
  90.  
  91.          // Public {{ ================================================================ //
  92.  
  93.          let self = {
  94.              modules: {
  95.  
  96.              },
  97.  
  98.              raiseNthItem: function (n) {
  99.                  if (n >= 1)
  100.                  {
  101.                      let item = clip.ring.splice(n, 1)[0];
  102.                      clip.ring.unshift(item);
  103.                      util.clipboardSet(item);
  104.                  }
  105.              },
  106.  
  107.              removeAllItem: function () {
  108.                  if (util.confirm(util.getLocaleString("removeItem"),
  109.                                   util.getLocaleString("confirmRemoveAllItem"), window))
  110.                  {
  111.                      clip.ring = [];
  112.                      util.clipboardClear();
  113.                      echo(util.getLocaleString("allItemRemoved"), TIMEOUT_NORMAL);
  114.                  }
  115.              },
  116.  
  117.              removeNthItem: function (n) {
  118.                  util.message(n);
  119.  
  120.                  if (util.confirm(util.getLocaleString("removeItem"),
  121.                                   util.getLocaleString("confirmRemoveItem") + "\n\n" + truncate(clip.ring[n]),
  122.                                   window))
  123.                  {
  124.                      clip.ring.splice(n, 1);
  125.                      if (n === 0)
  126.                          util.clipboardClear();
  127.                      echo(util.getLocaleString("itemRemoved"), TIMEOUT_NORMAL);
  128.                  }
  129.              },
  130.  
  131.              openStatusBarMenu: function (ev) {
  132.                  $("clipple-menu").openPopup(ev.target, "after_start", 0, 0, true);
  133.              },
  134.  
  135.              onStatusBarMenuShowing: function (ev) {
  136.                  clip.sync();
  137.  
  138.                  let menus = [
  139.                      $("clipple-statusbar-menu-raise-menu"),
  140.                      $("clipple-statusbar-menu-delete-menu")
  141.                  ];
  142.  
  143.                  let disabled = (clip.ring.length === 0);
  144.  
  145.                  menus.forEach(function (elem) {
  146.                                    if (disabled)
  147.                                    {
  148.                                        elem.setAttribute("disabled", true);
  149.                                        elem.setAttribute("tooltiptext", util.getLocaleString("clipboardEmpty"));
  150.                                    }
  151.                                    else
  152.                                    {
  153.                                        elem.removeAttribute("disabled");
  154.                                        elem.removeAttribute("tooltiptext");
  155.                                    }
  156.                                });
  157.              },
  158.  
  159.              onDeleteMenuShowing: function (ev) {
  160.                  let popup = $("clipple-statusbar-menu-delete-popup");
  161.                  updateDeleteMenuPopup(popup);
  162.              },
  163.  
  164.              onRaiseMenuShowing: function (ev) {
  165.                  let popup = $("clipple-statusbar-menu-raise-popup");
  166.                  removeAllChilds(popup);
  167.                  updateActionMenuPopup(popup, "clippleStatusbar.raiseNthItem(%s)");
  168.              }
  169.          };
  170.  
  171.          // }} ======================================================================= //
  172.  
  173.          init();
  174.  
  175.          return self;
  176.      })();
  177.